package com.cmcc.groupcontacts.common;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.telephony.PhoneNumberUtils;

import com.cmcc.groupcontacts.firewall.business.FilterListBusiness;
import com.cmcc.groupcontacts.firewall.dao.model.FilterListVO;
import com.cmcc.groupcontacts.util.ValidationUtil;

/**
 * 广播接收器抽象基类
 * 
 * @author
 * 
 */
public abstract class BaseReceiver extends BroadcastReceiver {
	/*
	 * 校验是否存在于黑名单中
	 */
	protected boolean isExistedInBlackList(String number) {
		// TODO: [优化]缓存黑名单，从缓存中获取数据比对
		FilterListBusiness filterBiz = new FilterListBusiness();
		
		ArrayList<FilterListVO> blackFilters = filterBiz.getFilterList(true);
		for (FilterListVO blackFilter : blackFilters) {
			if (PhoneNumberUtils.compare(number, blackFilter.getNumber())) {
				return true;
			}
		}
		
		ArrayList<FilterListVO> blackAreaCodeFilters = filterBiz.getFilterList(FilterListVO.TYPE_BLACK_AREA_CODE);
		for (FilterListVO blackFilter : blackAreaCodeFilters) {
			if(number.startsWith(blackFilter.getNumber())){
				return true;
			}
		}
		
		ArrayList<FilterListVO> blackAreaFilters = filterBiz.getFilterList(FilterListVO.TYPE_BLACK_AREA);
		String area = filterBiz.getAreaNameByNumber(number);
		for (FilterListVO blackFilter : blackAreaFilters) {
			if(area.equals(blackFilter.getNumber())){
				return true;
			}
		}
		return false;
	}

	/*
	 * 校验是否存在于白名单中
	 */
	protected boolean isExistedInWhiteList(String number) {
		// TODO: [优化]缓存白名单，从缓存中获取数据比对
		ArrayList<FilterListVO> whiteFilters = new FilterListBusiness().getFilterList(false);
		for (FilterListVO blackFilter : whiteFilters) {
			if (PhoneNumberUtils.compare(number, blackFilter.getNumber())) {
				return true;
			}
		}
		return false;
	}

	/*
	 * 校验是否存在于本地通信录中
	 */
	protected boolean isExistedInLocalContacts(String number) {
//		List<ContactIndex> localContacts = GroupContactsActivity.localContacts;
//		if (ValidationUtil.isNullOrEmpty(localContacts)) {
//			localContacts = MainDialActivity.localContacts;
//		}
//		for (ContactIndex contact : localContacts) {
//			if (PhoneNumberUtils.compare(number, contact.getUserPhone())) {
//				return true;
//			}
//		}
		return false;
	}

	/*
	 * 校验是否存在于本地和集团通信录中
	 */
	protected boolean isExistedInAllContacts(String number) {
//		List<ContactIndex> allContacts = GroupContactsActivity.allContacts;
//		if (ValidationUtil.isNullOrEmpty(allContacts)) {
//			allContacts = MainDialActivity.allContacts;
//		}
//		for (ContactIndex contact : allContacts) {
//			if (PhoneNumberUtils.compare(number, contact.getUserPhone())) {
//				return true;
//			}
//		}
		return false;
	}
}
